'''
    Rockaby: a text annotation system
    Copyright 2010 Martin Paul Eve

    This file is part of Rockaby.

    Rockaby is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Rockaby is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with Rockaby.  If not, see <http://www.gnu.org/licenses/>.

'''

#from rockaby.models import Episode, Character, ReferenceItem
import re

class utils(object):


    def __init__(self):
        '''
        Constructor
        '''
    @staticmethod
    def styleCharacters(characters, text):
        replacement = text
        for character in characters:
            regex = re.compile( '\\b(?P<charmatch>' + character.regex + '(\'s)?)\\b(?![^\[]+\\"\])')
            replacement = regex.sub('<span class="blacklink"><a href="/characters/' + character.name + '/" title="' + character.name + '">\g<charmatch></a></span>', replacement)
            
        return replacement
    
    @staticmethod
    def styleIndex(indices, text):
        replacement = text
        for index in indices:
            regex = re.compile( '\\b(?P<charmatch>' + index.regex + '(\'s)?)\\b(?![^\[]+\\"\])')
            replacement = regex.sub('<span class="blacklink"><a href="/reference/' + index.name + '/" title="' + index.name + '">\g<charmatch></a></span>', replacement)
            
        return replacement
         
    @staticmethod   
    def sanitize(input):
        ret = input.replace(' ', '_')
        ret = ret.replace('.', '[dot]')
        
        return ret
    
    @staticmethod
    def applystyle(input):
        replacement = input;
        replacement = replacement.replace('[italic]', '<span class="italic">')
        replacement = replacement.replace('[/italic]', '</span>')

        replacement = replacement.replace('\r\n', '</span></p><p class="p1"><span>')

        return replacement
        